﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;

using Microsoft.AspNetCore.Mvc;

using Xc.DataAccess.Core;
using Xc.DataAccess.DataBase.DeviceInfos;
using Xc.DataAccess.DataBase.DeviceInfos.Models;

namespace Xc.WaterMeters.ServerHosts.Controllers
{
    /// <summary>
    /// 设备信息属性控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DeviceInfoPropertyController : ControllerBase
    {
        /// <summary>
        /// 设备属性服务
        /// 
        /// </summary>
        private readonly IDbDeviceInfoPropertyService dbDeviceInfoPropertyService;
        /// <summary>
        /// 设备信息属性控制器
        /// </summary>
        /// <param name="dbDeviceInfoPropertyService"></param>
        public DeviceInfoPropertyController(IDbDeviceInfoPropertyService dbDeviceInfoPropertyService)
        {
            this.dbDeviceInfoPropertyService = dbDeviceInfoPropertyService;
        }
        /// <summary>
        /// 设置属性
        /// </summary>
        /// <param name="valuePairs"></param>
        /// <param name="deviceId"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> SetProperties([FromBody]Dictionary<string, UnitString> valuePairs, string deviceId)
        {
          return await  dbDeviceInfoPropertyService.Set(valuePairs, deviceId);
        }
        /// <summary>
        /// 删除指定属性信息
        /// </summary>
        /// <param name="deviceId">设备编号</param>
        /// <param name="name">属性名称</param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<Result> Delete(string deviceId,string name)
        {
            return await dbDeviceInfoPropertyService.Remove(deviceId,name);
        }
        /// <summary>
        /// 获取设备属性
        /// </summary> 
        /// <returns></returns>
        [HttpGet]
        public  ResultData<DevicePropertyInfo[]> GetProperties(string deviceId)
        {
            return dbDeviceInfoPropertyService.Gets(deviceId);
        }
        /// <summary>
        /// 历史属性星系
        /// </summary>
        /// <param name="deviceId"></param>
        /// <param name="dateTimeStart"></param>
        /// <param name="dateTimeEnd"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<DevicePropertyInfoHistory[]> GetPropertyHistory(string deviceId, DateTime? dateTimeStart, DateTime? dateTimeEnd)
        {
            return await dbDeviceInfoPropertyService.Gets(deviceId, dateTimeStart,dateTimeEnd);
        }
        /// <summary>
        /// 获取属性表
        /// </summary>
        /// <param name="deviceId">设备编号</param>
        /// <param name="autoAppendLastState">自动补齐最新状态</param>
        /// <param name="dateTimeStart">开始时间</param>
        /// <param name="dateTimeEnd">结束时间</param>
        /// <returns></returns>
        [HttpGet()]
        public async Task<DataTableInfo> GetPropertiesTable(string deviceId, DateTime? dateTimeStart, DateTime? dateTimeEnd,bool autoAppendLastState= true)
        {
            return await dbDeviceInfoPropertyService.GetTable(deviceId, autoAppendLastState, dateTimeStart,dateTimeEnd);
        }
    }
}
